using DevExpress.Data.ODataLinq;
using DevExpress.Mvvm;
using DevExpress.Xpf.Grid;
using DevExpress.XtraEditors.DXErrorProvider;
using DXApplications.Models;
using DXApplications.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DXApplications.Views
{
    /// <summary>
    /// Interaction logic for TyprManage.xaml
    /// </summary>
    public partial class TyprManage : UserControl
    {
        private TyprManageViewModel viewModel ;
        public TyprManage()
        {
            InitializeComponent();
            this.viewModel = this.DataContext as TyprManageViewModel;
           
        }

        private void ButtonInfo_Click(object sender, RoutedEventArgs e)
        {
            // 获取焦点行
            TypesModel focusedRow = (TypesModel)gridControl1.SelectedItem;
            
            if (focusedRow != null)
            {
                // 直接执行删除操作，因为我们知道这是删除按钮的点击事件
                viewModel.DeleteFileType(focusedRow);
            }
        }

        private void TableView_ValidateRow(object sender, GridRowValidationEventArgs e)
        {
            TypesModel rowData = e.Row as TypesModel;
            if (rowData == null) {
                e.IsValid = false;
                return;
            } 

            // 验证逻辑（示例：检查必填字段）
            if (string.IsNullOrEmpty(rowData.FileTypeName))
            {
                e.IsValid = false;
                e.ErrorContent = "姓名不能为空";
                e.ErrorType = ErrorType.Critical;
            }
            else 
            {
                try
                {
                    if(viewModel.IsFileTypeValid(rowData))
                    {
                        e.IsValid = true;
                    }
                 
                }
                catch (Exception ex)
                {
                    e.IsValid = false;
                    e.ErrorContent = ex.Message;
                    e.ErrorType = ErrorType.Critical;
                }
            }
        }

        private void TableView_RowUpdated(object sender, RowEventArgs e)
        {
            try
            {
                TypesModel res = e.Row as TypesModel;
                if (res == null) return;
                viewModel.AddOrUpdate(res);
            }
            catch (Exception ex)
            {
                // 处理异常
                MessageBox.Show($"更新行时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void TableView_InitNewRow(object sender, InitNewRowEventArgs e)
        {
            // 获取新行对象（假设数据源是 ObservableCollection<TypesModel>）
            //TypesModel newRow = e.RowHandle as TypesModel;
            //if (newRow != null)
            //{
            //    // 设置默认值
            //    // 示例：设置唯一ID
            //    newRow.FileTypeName = "默认名称";
            //    newRow.MemoryStrength = 24;

            //}
        }
       

        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            TypesModel  typesModel = new TypesModel
            {
                FileTypeName = typTextEdit.Text,
                MemoryStrength = 24 // 设置默认记忆强度
            };
            viewModel.AddFileType(typesModel);
            viewModel.UpdateFileTypes();
        }

        private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if(e.NewValue is bool isVisible && isVisible)
            {
                viewModel.UpdateFileTypes();
            }
           
        }
    }
}
